package com.junmoo.www.auth.module.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.junmoo.www.auth.module.system.service.SysUserService;
import com.junmoo.www.entity.SysUser;
import com.junmoo.www.tool.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.server.ServerRequest;
import reactor.core.publisher.Mono;
import java.util.List;


@RestController
@RequestMapping("/user")
@Slf4j
@AllArgsConstructor
public class SysUserController {
    private final SysUserService sysUserService;
    @GetMapping("/findAll")
    public Mono<Result> findAll(final ServerHttpRequest request){
        String token = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
        System.out.println("----token------"+ token);
        List<SysUser> users = sysUserService.findAll();
        return Mono.just(Result.success(users));
    }
    @GetMapping("/findById/{id}")
    public Mono<Result> findById(@PathVariable("id") Long id){
        SysUser user = sysUserService.findOneByAnd(new SysUser().setId(id));
        return Mono.just(Result.success(user));
    }
    @GetMapping("/findByName/{name}")
    public Mono<Result> findByName(String name){
        SysUser user = sysUserService.findOneByAnd(new SysUser().setUsername(name));
        return Mono.just(Result.success(user));
    }
    @GetMapping("/findOne")
    public Mono<Result> findOne(SysUser entity){
        SysUser user = sysUserService.findOneByAnd(entity);
        return Mono.just(Result.success(user));
    }

    @GetMapping("/page")
    public Mono<Result> page(SysUser search, int currentPage, int Pagesize, String sortField, String sortOrder){
        IPage<SysUser> page = sysUserService.page(search,currentPage,Pagesize,sortField,sortOrder);
        return Mono.just(Result.success(page));
    }

//    @ApiOperation("新增")
//    @PostMapping()
//    public Result create(@RequestBody  SysUser entity){
//        return  sysUserService.create( entity);
//    }
//    @ApiOperation("编辑")
//    @PutMapping()
//    public Result edit(@RequestBody  SysUser entity){
//        return  sysUserService.edit( entity);
//    }
//    @ApiOperation("删除")
//    @DeleteMapping()
//    public Result deleteByIds(@RequestBody List<Number> ids){
//        return  sysUserService.deleteByIds( ids);
//    }
//    @ApiOperation("授予角色")
//    @PostMapping("/assignRole/{userId}")
//    public Result assignRole(@PathVariable("userId") Number userId , @RequestBody List<Number> roleIds){
//        return  sysUserService.assignRole( userId,roleIds);
//    }
}
